﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKRGeometry;

namespace YJKStairs.HYElevator.Kit
{
	internal class FilterWallByEachLayer
	{
		public Dictionary<ElementId, List<WallLocation>> DicWallEachLayer
		{
			get
			{
				return this.m_DicWallEachLayer;
			}
		}

		private FilterWallByEachLayer(Autodesk.Revit.DB.Document Doc, List<ViewPlan> FloorPlan)
		{
			this.m_Doc = Doc;
			this.m_FloorPlan = FloorPlan;
		}

		public void Execute()
		{
			this.m_DicWallEachLayer = new Dictionary<ElementId, List<WallLocation>>();
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));
			foreach (ViewPlan viewPlan in this.m_FloorPlan)
			{
				double elevation = viewPlan.GenLevel.Elevation;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc, viewPlan.Id);
				filteredElementCollector.WherePasses(elementClassFilter).WhereElementIsNotElementType();
				List<WallLocation> list = new List<WallLocation>();
				foreach (Element element in filteredElementCollector)
				{
					Wall wall;
					if ((wall = (element as Wall)) != null)
					{
						Curve curve = (wall.Location as LocationCurve).Curve;
						double z = curve.GetEndPoint(0).Z;
						double val = 0.0;
						double num = 0.0;
						this.GetWallTopAndBottomElevator(element.Id, ref val, ref num);
						if (!Geometry.LessThan(elevation, val) && Geometry.GreaterThan(num - elevation, 1.0))
						{
							WallLocation item;
							item.WallId = wall.Id;
							Transform transform = Transform.CreateTranslation(XYZ.BasisZ * (elevation - z));
							item.curve = curve.CreateTransformed(transform);
							list.Add(item);
						}
					}
				}
				this.m_DicWallEachLayer.Add(viewPlan.GenLevel.Id, list);
			}
		}

		public static FilterWallByEachLayer GetInstance(Autodesk.Revit.DB.Document Doc, List<ViewPlan> FloorPlan)
		{
			if (FilterWallByEachLayer.m_FilterWall == null)
			{
				FilterWallByEachLayer.m_FilterWall = new FilterWallByEachLayer(Doc, FloorPlan);
				FilterWallByEachLayer.m_FilterWall.Execute();
			}
			return FilterWallByEachLayer.m_FilterWall;
		}

		public static void DisposInstance()
		{
			if (FilterWallByEachLayer.m_FilterWall != null)
			{
				FilterWallByEachLayer.m_FilterWall = null;
			}
		}

		private void GetWallTopAndBottomElevator(ElementId wallid, ref double baseLevelValue, ref double topLevelValue)
		{
			Wall wall = this.m_Doc.GetElement(wallid) as Wall;
			ElementId elementId = wall.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId();
			Level level = this.m_Doc.GetElement(elementId) as Level;
			double num = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).AsDouble();
			double num2 = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();
			baseLevelValue = level.Elevation + num;
			topLevelValue = baseLevelValue + num2;
		}

		private Document m_Doc;

		private List<ViewPlan> m_FloorPlan;

		private Dictionary<ElementId, List<WallLocation>> m_DicWallEachLayer;

		public static FilterWallByEachLayer m_FilterWall;
	}
}
